package facade;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import Entidade.Usuario;
import VO.UsuarioVO;
import org.apache.catalina.realm.RealmBase;

public class FacadeusuarioImpl implements FacadeUsuario {

	public static String entidade ="sistemaDarlan";
	
	public void AlterarUsuario(UsuarioVO u) {
		Usuario m = new Usuario();
		    m.setId(u.getId());
		    
		    if(u.getLogin().equalsIgnoreCase("Darlan")){
		    	m.setPerfil("admin");
		    }else{
		    	m.setPerfil("user");
		    }
		    
			m.setNome(u.getNome());
			m.setLogin(u.getLogin());
			m.setSenha(RealmBase.Digest(u.getSenha(), "MD5", "UTF-8"));
		
		  try{
			  EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		      manager.getTransaction().begin();
		 
				manager.merge(m);
		
		    manager.getTransaction().commit();
		    manager.close();
		    
		  }catch  (Exception e){
			    e.getMessage();
			
		  }
	
	}

	public void SalvarUsuario(UsuarioVO u) throws Exception {
		
		
		//senha = RealmBase.Digest(senha, "MD5", "UTF-8");

			Usuario m = new Usuario();
		    m.setId(u.getId());
		    if(u.getLogin().equalsIgnoreCase("Darlan")){
		    	m.setPerfil("admin");
		    }else{
		    	m.setPerfil("user");
		    }
		    	m.setLogin(u.getLogin());	
				m.setNome(u.getNome());
				m.setSenha(RealmBase.Digest(u.getSenha(), "MD5", "UTF-8"));
			
			
			  try{
				  EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
				manager.getTransaction().begin();
			
				manager.persist(m);
				
			    manager.getTransaction().commit();
			    manager.close();
			    
			  }catch  (Exception e){
				    e.getMessage();
				
			  }
			
	
		
	}

	public UsuarioVO buscaUsuarioPeloID(Integer id)  throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<UsuarioVO> buscaTodosUsuario() throws Exception {
		try{
		    EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
			Query q = manager.createQuery("from Usuario u");
		
			List<UsuarioVO>  mVo= new ArrayList<UsuarioVO>(); 
			
			List<Usuario> resultList =q.getResultList();
			
			for(Usuario m : resultList){
				UsuarioVO vo = new UsuarioVO();
				vo.setId(m.getId());
				vo.setLogin(m.getLogin());
				vo.setNome(m.getNome());
				vo.setPerfil(m.getPerfil());
				vo.setSenha(m.getSenha());
				
				mVo.add(vo);
			}
				
			
			
			return mVo;
			
		}catch (Exception e){
			e.getMessage();
		}
		return null;
	}

	public void excluirUsuario(UsuarioVO u) throws Exception {
		Usuario m = new Usuario();
		
		try{
			  EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		     manager.getTransaction().begin();
		     
		   
		    m = manager.find(Usuario.class,u.getId());
		     
		     
		     manager.merge(m);
		     manager.remove(m);
		   
		    manager.getTransaction().commit();
		    manager.close();
		    
		  
		  }catch  (Exception e){
			    e.getMessage();
			
		  }
		
		
	}

	
}
